home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / doc / python-apt / examples / build-deps.py < prev    next >
Encoding:
Python Source  |  2009-03-30  |  2.0 KB  |  74 lines

  1. #!/usr/bin/python
  2. # this is a example how to access the build dependencies of a package
  3.  
  4. import apt_pkg
  5. import sys
  6.  
  7.  
  8. def get_source_pkg(pkg, records, depcache):
  9.     """ get the source package name of a given package """
  10.     version = depcache.GetCandidateVer(pkg)
  11.     if not version:
  12.         return None
  13.     file, index = version.FileList.pop(0)
  14.     records.Lookup((file, index))
  15.     if records.SourcePkg != "":
  16.         srcpkg = records.SourcePkg
  17.     else:
  18.         srcpkg = pkg.Name
  19.     return srcpkg
  20.  
  21.  
  22. # main
  23. apt_pkg.init()
  24. cache = apt_pkg.GetCache()
  25. depcache = apt_pkg.GetDepCache(cache)
  26. depcache.Init()
  27. records = apt_pkg.GetPkgRecords(cache)
  28. srcrecords = apt_pkg.GetPkgSrcRecords()
  29.  
  30. # base package that we use for build-depends calculation
  31. if len(sys.argv) < 2:
  32.     print "need a package name as argument"
  33.     sys.exit(1)
  34. try:
  35.     base = cache[sys.argv[1]]
  36. except KeyError:
  37.     print "No package %s found" % sys.argv[1]
  38.     sys.exit(1)
  39. all_build_depends = set()
  40.  
  41. # get the build depdends for the package itself
  42. srcpkg_name = get_source_pkg(base, records, depcache)
  43. print "srcpkg_name: %s " % srcpkg_name
  44. if not srcpkg_name:
  45.     print "Can't find source package for '%s'" % pkg.Name
  46. srcrec = srcrecords.Lookup(srcpkg_name)
  47. if srcrec:
  48.     print "Files:"
  49.     print srcrecords.Files
  50.     bd = srcrecords.BuildDepends
  51.     print "build-depends of the package: %s " % bd
  52.     for b in bd:
  53.         all_build_depends.add(b[0])
  54.  
  55. # calculate the build depends for all dependencies
  56. depends = depcache.GetCandidateVer(base).DependsList
  57. for dep in depends["Depends"]: # FIXME: do we need to consider PreDepends?
  58.     pkg = dep[0].TargetPkg
  59.     srcpkg_name = get_source_pkg(pkg, records, depcache)
  60.     if not srcpkg_name:
  61.         print "Can't find source package for '%s'" % pkg.Name
  62.         continue
  63.     srcrec = srcrecords.Lookup(srcpkg_name)
  64.     if srcrec:
  65.         #print srcrecords.Package
  66.         #print srcrecords.Binaries
  67.         bd = srcrecords.BuildDepends
  68.         #print "%s: %s " % (srcpkg_name, bd)
  69.         for b in bd:
  70.             all_build_depends.add(b[0])
  71.  
  72.  
  73. print "\n".join(all_build_depends)
  74.